/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zonacardio.servlest;

import com.zonacardio.entidad.Plan;
import com.zonacardio.entidad.VentaPlanes;
import com.zonacardio.negocio.PlanN;
import com.zonacardio.negocio.VentaPlanN;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Daniel
 */
public class ReportesServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String targetPage = "./Principal.jsp";
        request.setAttribute("target", "./FPrincipal.jsp");
        request.setAttribute("targetModulo", "./FReportes.jsp");
        // parametor para la consulta
        String id = request.getParameter("txtideportista");
        String codplan = request.getParameter("codplan");
        String fechai = request.getParameter("fechai");
        String fechaf = request.getParameter("fechaf");
        String estado = request.getParameter("estado");

        VentaPlanN negocioU = new VentaPlanN();
        VentaPlanes entVP = new VentaPlanes();
        PlanN negocioP = new PlanN();
        Plan entP = new Plan();
        request.setAttribute("ListaPlanes", null);
        request.setAttribute("plan", null);
        request.setAttribute("mensaje", null);
        List<Plan> ListaPlanes = negocioP.ListadoDePlanes();
        request.setAttribute("ListaPlanes", ListaPlanes);
        String cadena="";
        if ("inicio".equals(request.getParameter("action"))) {
          try {


                List<VentaPlanes> ListaVentaPlan = negocioU.ReporteDeVentaPlanes(cadena);
                 request.setAttribute("ListaVentaPlanes", ListaVentaPlan);

            } catch (Exception ex) {
                Logger.getLogger(ReportesServlet.class.getName()).log(Level.SEVERE, null, ex);
                request.setAttribute("mensaje", ex.getMessage());
            }
        }

         if ("imprimir".equals(request.getParameter("action"))) {
          try {

              if((fechai.length()!=0)&& (fechaf.length()!=0)  ){
                  cadena+= " AND STR_TO_DATE(vp.fechaventa, '%d-%m-%Y')"
                          + " >=STR_TO_DATE('"+ fechai +"', '%d-%m-%Y') ";
                  cadena+=" AND  STR_TO_DATE(vp.fechaventa, '%d-%m-%Y')"
                          + " <=STR_TO_DATE('"+ fechaf +"', '%d-%m-%Y') ";

                }


              if(id.length()!=0){
                cadena+=" AND d.numerodocumento= '"+id +"'   ";

                }

               if(codplan.length()!=0){
                cadena+=" AND vp.codigoplanes = '"+codplan +"'   ";


                }


                if(estado.length()!=0){
                cadena+=" AND vp.estado = '"+estado +"'   ";

                }


                List<VentaPlanes> ListaVentaPlan = negocioU.ReporteDeVentaPlanes(cadena);
               request.setAttribute("ListaVentaPlanes", ListaVentaPlan);
               request.setAttribute("targetModulo", "./Fimprimir.jsp");

            } catch (Exception ex) {
                Logger.getLogger(ReportesServlet.class.getName()).log(Level.SEVERE, null, ex);
                request.setAttribute("mensaje", ex.getMessage());
            }
        }




     request.getRequestDispatcher(targetPage).forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

